package com.jxmcloud.business.cut.service.impl.user;


import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutSaveKey;
import com.jxmcloud.business.cut.dto.admin.AdminUserMergerDto;
import com.jxmcloud.business.cut.dto.client.LoginNameDto;
import com.jxmcloud.business.cut.enums.UserSexEnum;
import com.jxmcloud.business.cut.enums.UserStatus;
import com.jxmcloud.business.cut.model.database.DBUserLoginInfo;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.database.IUserLoginInfoDBService;
import com.jxmcloud.business.cut.service.user.IUserBaseService;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.util.XuanleRpcUtil;
import com.jxmcloud.business.cut.vo.toDb.ToDbUserBase;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserBaseVo;
import com.xlcloud.platform.util.DateUtil;
import com.xlcloud.platform.util.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserBaseServiceImpl implements IUserBaseService {
    @Autowired
    private IUserLoginInfoDBService userLoginInfoDBService;

    @Autowired
    private IUserActivityInfoDBService userActivityInfoDBService;

    @Autowired
    private XuanleRpcUtil xuanleRpcUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveToDb(PlayerInfoVo player, String key) {
        UserBaseVo data = player.getBase();
        ToDbUserBase toDbUserBase = new ToDbUserBase();
        if (key.equals(CutSaveKey.USER_BASE_STATUS)) {
            toDbUserBase.setStatus(data.getStatus());
            toDbUserBase.setUid(data.getUid());
            if (data.getBannedTime() > 0) {
                toDbUserBase.setBannedTime(data.getBannedTime() + "");
            }
            return userLoginInfoDBService.updateUserStatusInfo(toDbUserBase);
        }
        if (key.equals(CutSaveKey.USER_BASE)) {
            toDbUserBase.setXlId(data.getXlId());
            toDbUserBase.setUid(data.getUid());
            toDbUserBase.setAccount(data.getAccount());
            toDbUserBase.setNickName(data.getNickName());
            toDbUserBase.setPhone(data.getPhone());
            toDbUserBase.setShopId(data.getShopId());
            toDbUserBase.setIcon(data.getIcon());
            toDbUserBase.setSex(data.getSex());
            toDbUserBase.setShopName(data.getShopName());
            toDbUserBase.setCreateTime(data.getCreateTime());
            toDbUserBase.setSession(data.getSession());
            toDbUserBase.setTotalBuyNum(player.getUserOrderListVo().getTotalBuyNum());
            return userLoginInfoDBService.updateUserBaseInfo(toDbUserBase);
        }
        return false;
    }

    @Override
    public boolean loadFromDb(PlayerInfoVo player, Integer uid) {
        DBUserLoginInfo dbUserLoginInfo = userLoginInfoDBService.selectOne(uid);
        if (ObjectUtil.isNotNull(dbUserLoginInfo)) {
            UserBaseVo base = player.getBase();
            base.setUid(dbUserLoginInfo.getUid());
            base.setXlId(dbUserLoginInfo.getXlId());
            base.setAccount(dbUserLoginInfo.getAccount());
            base.setNickName(dbUserLoginInfo.getNickName());
            base.setIcon(dbUserLoginInfo.getIcon());
            base.setShopId(dbUserLoginInfo.getShopId());
            base.setShopName(dbUserLoginInfo.getShopName());
            base.setSex(dbUserLoginInfo.getSex());
            base.setPhone(dbUserLoginInfo.getPhone());
            base.setCreateTime(MyTool.getCurrentTimeSecond(dbUserLoginInfo.getRegisterTime().getTime()));
            return true;
        }
        return false;
    }

    @Override
    public boolean register(PlayerInfoVo player, Integer uid) {
        // 无
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createByAccount(PlayerInfoVo player, String account) {
        // 创建玩家用户信息
        DBUserLoginInfo userLoginInfo = DBUserLoginInfo.builder().uid(player.getBase().getUid())
                .xlId(player.getBase().getXlId())
                .account(account)
                .nickName(player.getBase().getNickName())
                .phone(player.getBase().getPhone())
                .shopId(player.getBase().getShopId())
                .icon(player.getBase().getIcon())
                .sex(player.getBase().getSex())
                .shopName(player.getBase().getShopName())
                .createTime(new Date())
                .updateTime(new Date())
                .registerTime(new Date(player.getBase().getCreateTime() * 1000L))
                //.totalBuyNum(0)
                .build();
        boolean flag = userLoginInfoDBService.insertIntoData(userLoginInfo);
        if (flag) {
            player.getBase().setUid(userLoginInfo.getUid());
            player.getBase().setAccount(account);
        }
        return flag;
    }

    @Override
    public void updateFromXlData(PlayerInfoVo player, LoginNameDto data) {
        UserBaseVo base = player.getBase();
        base.setUid(player.getBase().getUid());
        base.setXlId(data.getUserId());
        base.setAccount(data.getLoginName());
        base.setNickName(data.getNickName());
        base.setPhone(data.getMobile());
        base.setShopId(data.getShopId());
        base.setIcon(data.getPicPath());
        base.setSex(UserSexEnum.getFlag(data.getSex()));
        base.setShopName(data.getShopName());
        base.setCreateTime(MyTool.getCurrentTimeSecond(data.getCreateTime()));
        player.getMaster().getSaveList().add(CutSaveKey.USER_BASE);
    }

    @Override
    public void makeSession(PlayerInfoVo player) {
        Integer uid = player.getBase().getUid();
        String sessionKey = MyTool.generateUserSessionKey(uid);
        player.getBase().setSession(sessionKey);
    }

    @Override
    public UserBaseVo getUserBase(PlayerInfoVo player) {
        return player.getBase();
    }

    @Override
    public void updateUserStatusData(PlayerInfoVo player, AdminUserMergerDto data) {
        UserBaseVo base = player.getBase();
        base.setStatus(data.getStatus());
        base.setBannedTime(0);
        if (!UserStatus.NORMAL.getValue().equals(data.getStatus()) && PublicUtil.isNotEmpty(data.getBannedTime()) && !data.getBannedTime().equals("-1")) {
            base.setBannedTime(Math.toIntExact(MyTool.toTimeByString(data.getBannedTime())));
        }
        player.getMaster().getSaveList().add(CutSaveKey.USER_BASE_STATUS);
    }

    @Override
    public void checkParam(String bannedTime) {
        if (ObjectUtil.isNotEmpty(bannedTime) && !bannedTime.equals("-1")) {
            PublicUtil.ifAndThrow(DateUtil.isAfterStart(bannedTime, DateUtil.getDateTime()), "时间应大于当前时间");
        }
    }
}
